package business
{
	import com.asfusion.mate.events.Dispatcher;
	
	import flash.events.Event;
	
	import models.misc.SettingsManager;
	
	import mx.formatters.DateFormatter;
	
	public class MiscUtil
	{
		private static var df:DateFormatter = new DateFormatter();
		public static var UrlRegExp:String = "(((http|ftp|https):\/\/){1}([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)+([\S,:\/\.\?=a-zA-Z0-9_-]+))";
		public function MiscUtil()
		{
		}
		public static function calculateDate(originDate:Date, different:Number, unit:String):Date{
			var diff:Number;
			var newDate:Date = new Date();
			switch(unit){
				case "second": 		diff = different * 1000; break;
				case "minute": 		diff = different * 60000; break;
				case "hour": 		diff = different * 3600000; break;
				case "day": 		diff = different * 86400000; break; 
				case "week":		diff = different * 604800000; break;
				case "month": 		diff = different * 2592000000; break; 
				case "year":		diff = different * 31536000000; break;
			};
			newDate.setTime( originDate.getTime() + diff );
			return newDate;
		}
		public static function dateDifferent(start:Date, end:Date):Number{
			return Math.ceil( (end.getTime() - start.getTime()) / Consts.MsInDay ); 
		}
		public static function sizeWithBorder(cellSize:Number, numberOfCell):Number{
			return (cellSize - 1) * numberOfCell;
		}
		public static function properTrim(str:String, len:Number):String{
			if( str.length > len ){
				var index:int = str.indexOf(" ", len-8);
				index = ( index == -1 ? len : index ); 
				return str.substring(0, index) + ' ...';
			}
			return str;
		}
		public static function formatDate(d:Date, formatStr:String = null):String{
			if( formatStr == null ){
				df.formatString = SettingsManager.getSetting('dateformat') as String;
			}else{
				df.formatString = formatStr;
			}
			
			return df.format(d);
		}
		public static function raiseEvent(evt:Event):void{
			(new Dispatcher).dispatchEvent(evt);
		}
		public static function checkIfEmpty(arr:Object):Boolean{
			if( arr == null ) return true;
			return (arr.length == 0); 
		}

	}
}